package com.dev.module_ui_composable.lce

sealed class AppUiState<out R> {
    fun isLoading() = this is AppUiLoading
    fun isSuccessful() = this is AppUiSuccess<*>
    fun isNoContent() = this is AppNoContent
    fun isError() = this is AppUiError

    override fun toString() = when (this) {
        is AppUiSuccess<*> -> "Success[data=$data]"
        is AppNoContent -> "NoContent[reason=$reason]"
        is AppUiError -> "Error[exception=${error?.localizedMessage ?: "unknown error"}]"
        AppUiLoading -> "Loading"
    }

    fun <T> successOrNull(): Any? = (this as? AppUiSuccess<*>)?.data
}

data class AppUiSuccess<out T>(val data: T) : AppUiState<T>()
data class AppNoContent(val reason: String) : AppUiState<Nothing>()
data class AppUiError(val error: Throwable?) : AppUiState<Nothing>()
object AppUiLoading : AppUiState<Nothing>()